package com.wy.w_ui.tab.top;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import com.wy.w_ui.R;
import com.wy.w_ui.tab.common.IWTab;
import org.jetbrains.annotations.NotNull;

public class WTabTop extends RelativeLayout implements IWTab<WTabTopInfo<?>> {

    private WTabTopInfo<?> tabTopInfo;
    private ImageView tabImage;
    private TextView tabName;
    private View indicator;

    public WTabTop(Context context) {
        this(context, null);
    }

    public WTabTop(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WTabTop(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    /**
     * 初始化view
     */
    private void init() {

        LayoutInflater.from(getContext()).inflate(R.layout.tab_top, this);
        tabImage = findViewById(R.id.iv_image);
        tabName = findViewById(R.id.tv_name);
        indicator = findViewById(R.id.tab_indicator);
    }


    @Override
    public void setTabInfo(@NotNull WTabTopInfo<?> WTabTopInfo) {
        this.tabTopInfo = WTabTopInfo;

        inflateInfo(false,true);
    }

    @Override
    public void restHeight(int height) {

        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        layoutParams.height = height;
        setLayoutParams(layoutParams);
        getTabName().setVisibility(GONE);
    }


    @Override
    public void onTabSelectChange(int index, @Nullable WTabTopInfo<?> prevInfo, @NotNull WTabTopInfo<?> nextInfo) {

        if (prevInfo != tabTopInfo && nextInfo != tabTopInfo || prevInfo == nextInfo){
            return;
        }

        if (prevInfo == tabTopInfo){
            inflateInfo(false,false);
        }else {
            inflateInfo(true,false);
        }
    }


    /**
     * 填充信息
     * @param select 选中状态
     * @param isFirst  是否第一次初始化
     */
    private void inflateInfo(boolean select, boolean isFirst) {
        //判断类型
        if (tabTopInfo.tabType == WTabTopInfo.TabType.TEXT){
            if (isFirst){
                tabImage.setVisibility(GONE);
                tabName.setVisibility(VISIBLE);
                if (!TextUtils.isEmpty(tabTopInfo.name)){
                    tabName.setText(tabTopInfo.name);
                }
            }

            if (select){
                tabName.setTextColor(getTextColor(tabTopInfo.selectedColor));
                indicator.setVisibility(VISIBLE);

            }else {
                tabName.setTextColor(getTextColor(tabTopInfo.defaultColor));
                indicator.setVisibility(GONE);
            }
        }else if (tabTopInfo.tabType == WTabTopInfo.TabType.BITMAP){

            if (isFirst){
                tabImage.setVisibility(VISIBLE);
                tabName.setVisibility(GONE);
            }

            if (select){
                tabImage.setImageBitmap(tabTopInfo.selectedBitmap);
            }else {
                tabImage.setImageBitmap(tabTopInfo.defaultBitmap);
            }

        }

    }


    @ColorInt
    private int getTextColor(Object color){

        if (color instanceof String){
            return Color.parseColor((String)color);
        }else {
            return ((int)color);
        }
    }

    public WTabTopInfo<?> getTabTopInfo() {
        return tabTopInfo;
    }

    public ImageView getTabImage() {
        return tabImage;
    }

    public TextView getTabName() {
        return tabName;
    }



}
